package com.ly.project_jt.controller;

import com.ly.project_jt.mapper.AdminMapper;
import com.ly.project_jt.pojo.Admin;
import com.ly.project_jt.pojo.AdminExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AdminController {
    @Autowired
    AdminMapper adminMapper;
//管理员登录
    @RequestMapping("/admin/login")
    @CrossOrigin
    public Admin login(Admin admin){
        //访问地址：http://localhost:1314/admin/login?adminName=root&adminPassword=root
        //where adminName='root' and adminPassword='root'
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria=adminExample.or();
        criteria.andAdminNameEqualTo(admin.getAdminName());
        criteria.andAdminPasswordEqualTo(admin.getAdminPassword());
        List<Admin> adminList=adminMapper.selectByExample(adminExample);
        if(adminList!=null &&adminList.size()>=1){
            Admin returnAdmin=adminList.get(0);
            //密码不能返回给浏览器，设置为******
            returnAdmin.setAdminPassword("******");
            return returnAdmin;
        }
        return null;
    }
}
